﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;

namespace iHub
{
    class dbConnector
    {
        private String security = "Integrated Security=SSPI;";
        private String server = System.Windows.Forms.SystemInformation.ComputerName;
        private String trust = "yes";
        private String database = "DB_IHUB";
        private SqlConnection con;
        private SqlTransaction transaction = null;

        public void connect()
        {
            try
            {
                con = new SqlConnection(security + "server=" + server + ";" + "Trusted_Connection=" + trust + ";" + "database=" + database + ";");
                con.Open();
            }
            catch
            {
                server = server + "\\sqlexpress";
                con = new SqlConnection(security + "server=" + server + ";" + "Trusted_Connection=" + trust + ";" + "database=" + database + ";");

                try
                {
                    con.Open();
                }
                catch (Exception err)
                {
                    throw new Exception(err.ToString());
                }
            }
        }

        public void disconnect()
        {
            try
            {
                con.Close();
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public SqlConnection getCon()
        {
            return con;
        }

        public void beginTransaction()
        {
            transaction = con.BeginTransaction(System.Data.IsolationLevel.Serializable);
        }

        public SqlTransaction getTransaction()
        {
            return transaction;
        }

        public void rollback()
        {
            transaction.Rollback();
        }

        public void commit()
        {
            transaction.Commit();
        }
    }
}